<template> 
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>活动专题详情</span><span class="warm-tip">(只需要维护1条数据即可，多条数据时，小程序只展示第一条)</span>
      <el-tooltip class="item" effect="dark" content="小程序端只展示第一条数据" placement="top-start">
        <el-button size="medium" type="primary" class="btn-add" @click="handleAdd()">添加专题详情</el-button>
      </el-tooltip>
    </el-card>
    <div class="choose-dept">
      <el-tabs v-model="activeDept" @tab-click="deptChange">
        <el-tab-pane v-for="item in depotList" :label="item.name" :name="item.name" :id="item.id" :key="item.id"></el-tab-pane>
      </el-tabs>
    </div>
    <div class="table-container">
      <el-table ref="homeAdvertiseTable"
                :data="list"
                style="width: 100%;"
                @selection-change="handleSelectionChange"
                v-loading="listLoading" border>
        <!-- <el-table-column type="selection" width="60" align="center"></el-table-column> -->
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>

        <el-table-column label="标题" width="500" align="center">
          <template slot-scope="scope">{{scope.row.title}}</template>
        </el-table-column>
        <!-- <el-table-column label="详情页展示图片" width="260" align="center">
          <template slot-scope="scope"><img style="max-width: 200px;max-height:100px;" :src="scope.row.thumbnail"></template>
        </el-table-column> -->

        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       
                       @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button size="mini"
                       type="danger"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
     <!-- 新增版块的弹窗 -->
    <el-dialog title="新增版块" :visible.sync="isNewPromotionOpen" :close-on-click-modal="false" :show-close="true" class="adjustmentBox">
      <el-form :model="newForm" style="width:25rem;" :rules="rules" >
          <el-form-item label="版块名称" prop="title" required label-width="5rem">
              <el-input v-model="newForm.title" placeholder="" ></el-input>
          </el-form-item>
          <el-form-item label="背景图片"  prop="pic" label-width="5rem">
              <single-upload v-model="newForm.pic"></single-upload>
          </el-form-item>
          <el-form-item label="选择仓库" required prop="depotId">
              <el-select v-model="newForm.depotId">
              <el-option
                  v-for="dept in depotList"
                  :key="dept.id"
                  :label="dept.name"
                  :value="dept.id">
              </el-option>
              </el-select>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="saveNewPromotion('newForm')" style="color:#fff;background-color:#417FF6;">保存</el-button>
          <el-button @click="isNewPromotionOpen = false">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 编辑版块的弹窗 -->
    <el-dialog title="版块分类" :visible.sync="isEditPromotionOpen" :close-on-click-modal="false" :show-close="true" class="adjustmentBox">
        <el-form :model="editForm" style="width:25rem;" :rules="rules" >
            <el-form-item label="版块名称" prop="title" required label-width="5rem">
                <el-input v-model="editForm.title" placeholder="" ></el-input>
            </el-form-item>
            <el-form-item label="背景图片"  prop="pic" label-width="5rem">
                <single-upload v-model="editForm.pic"></single-upload>
            </el-form-item>
            <el-form-item label="选择仓库" required prop="depotId">
                <el-select v-model="editForm.depotId">
                <el-option
                    v-for="dept in depotList"
                    :key="dept.id"
                    :label="dept.name"
                    :value="dept.id">
                </el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="排序"  prop="sort" label-width="5rem">
                <el-input v-model="editForm.sort" placeholder="" ></el-input>
            </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="saveEditPromotion('editForm')" style="color:#fff;background-color:#417FF6;">保存</el-button>
            <el-button @click="isEditPromotionOpen = false">关闭</el-button>
        </div>
    </el-dialog>
    <!-- <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div> -->
  </div>

</template>
<script>
  import {fetchList,createPromotion,updatePromotion,delPromotion,
    getPromotionDetailList,delPromotionDetail
  } from '@/api/promotion';
//   import {fetchList as fetchDeptList} from '@/api/depository'
  import {formatDate} from '@/utils/date';
  import SingleUpload from '@/components/Upload/singleUpload'
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 5,
    depotId:null,
    name: null,
    //type: 1,
    endTime:null
  };
  export default {
    name: 'homePromotionList',
     components:{SingleUpload},
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        multipleSelection: [],
        operates: [
          {
            label: "删除",
            value: 0
          }
        ],
        newForm:{
          title:"",
          pic:"",
          depotId:null
        },
        editForm:{
          title:"",
          pic:"",
          depotId:null,
          id:null
        },
        rules:{
          title:[
            { required: true,  message: '版块名称不能为空', trigger: ['blur','change']}
          ],
          pic:[
            { required: true,  message: '背景图片不能为空', trigger: ['blur','change']}
          ],
          depotId:[
            { required: true,  message: '仓库不能为空', trigger: ['blur','change']}
          ],
        },
        operateType: null,
        depotList:[],//仓库列表
        activeDept:null,//选中的仓库name
        isNewPromotionOpen:false,//新增弹出框
        isEditPromotionOpen:false,//编辑弹出框
      }
    },
    created() {
      this.getList();
    },
    filters:{
      formatType(type){
        if(type===1){
          return 'APP首页轮播';
        }else{
          return 'PC首页轮播';
        }
      },
      formatTime(time){
        if(time==null||time===''){
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
    },
    methods: {
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSelectionChange(val){
        this.multipleSelection = val;
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleDelete(index,row){
        this.deleteHomePromotion(row.id);
      },
      handleBatchOperate(){
        if (this.multipleSelection < 1) {
          this.$message({
            message: '请选择一条记录',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        if(this.operateType===0){
          //删除
          this.deleteHomePromotion(ids);
        }else {
          this.$message({
            message: '请选择批量操作类型',
            type: 'warning',
            duration: 1000
          });
        }
      },
      handleAdd(){
        this.$router.push({path: '/hds/addPromotionDetail', query:{pid:this.$route.query.id,depotId:this.$route.query.depotId}})
        //this.isNewPromotionOpen = true;
      },
      saveNewPromotion(){
        let self = this;
        createPromotion(self.newForm).then(res=>{
          if(res.code ==200){
            self.$message({
              message:"保存成功",
              type:"success",
              duration:1000
            })
            self.isNewPromotionOpen = false;
            self.getList();
          }
        })
      },
      handleUpdate(index,row){
        this.$router.push({path: '/hds/updatePromotionDetail', query: {pid:this.$route.query.id,id: row.id,depotId:this.$route.query.depotId}})
        //  this.isEditPromotionOpen = true;
        //  this.editForm = Object.assign({},row);
        //  let depotName = this.activeDept;
        //   for(const depot of this.depotList){
        //       if(depot.name === depotName){
        //           this.editForm.depotId = depot.id;
        //           break
        //       }
        //   }
      },
      saveEditPromotion(){
        let self = this;
        updatePromotion(self.editForm).then(res=>{
          if(res.code ==200){
            self.$message({
              message:"修改成功",
              type:"success",
              duration:1000
            })
            self.isEditPromotionOpen = false;
            self.getList();
          }
        })
      },
      deptChange(tab, event){
        let self = this;
        let depotId = tab.$attrs.id;
        self.listQuery.depotId = depotId;
        fetchList(depotId,this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        })
      },
      getList() {
        let self = this;
        this.listLoading = true;
          getPromotionDetailList(this.$route.query.id).then(response => {
            this.listLoading = false;
            if(response.code ==200 && response.data){
              this.list = response.data;
              this.total = response.data.total;
            }
          })
      },
      deleteHomePromotion(id){
        this.$confirm('是否要删除该版块?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // let params=new URLSearchParams();
          // params.append("ids",ids);
          delPromotionDetail(id).then(response=>{
            if(response.code ==200){
              this.getList();
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
            }
          });
        })
      }
    }
  }
</script>
<style scoped>
  .btn-add{
    margin-top: -8px;
  }
  .choose-dept >>> .el-tabs__item{
    font-size: 16px; 
  }
  .choose-dept >>> .el-tabs__header{
    margin: 15px 0 0;
  }
  .input-width {
    width: 203px;
  }

   /* 弹出框的宽度 */
.adjustmentBox >>> .el-dialog{
  width: 28rem;
} 
/* .warm-tip{
  font-size: 14px;
  color: #999;
  margin-left: 10px;
} */
</style>